﻿@using SmartCode
@using SmartCode.Db
@model BuildContext
@{
    var project = Model.Project;
    var dbSource = Model.GetDataSource<DbSource>();
    var table = Model.GetCurrentTable();
    var pkCol = table.PKColumn;
    if (pkCol == null)
    {
        throw new SmartCodeException($"{table.Name} can not find PKColumn！");
    }
    var autoIncrement = table.AutoIncrement;
    var useGK = autoIncrement ? $"useGeneratedKeys=\"true\" keyProperty=\"{pkCol.ConvertedName}\"" : "";
}

<insert id="insert" @useGK>
    insert into @table.Name
    (
    @for (var colIndex = 0; colIndex < table.Columns.Count(); colIndex++)
    {
        var col = table.Columns.ElementAt(colIndex);
        @if (!col.AutoIncrement)
        {
            @Html.PadLeft(6)@col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
            @Html.NewLine()
        }
    }
    )
    values
    (
    @for (var colIndex = 0; colIndex < table.Columns.Count(); colIndex++)
    {
        var col = table.Columns.ElementAt(colIndex);
        @if (!col.AutoIncrement)
        {
            @Html.PadLeft(6)<text>#{@col.ConvertedName}</text>
            @(colIndex == table.Columns.Count() - 1 ? "" : ",")
            @Html.NewLine()
        }
    }
    )
</insert>